package com.example.cmpe277stockdata.dataaccess;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.cmpe277stockdata.DataBaseHelper;
import com.example.cmpe277stockdata.beans.UserPortfolio;
import com.example.cmpe277stockdata.constants.DataConstants;

public class PortFolioDB 
{
	//Column Names
	public static final String KEY_ROWID ="_id";
	public static final String KEY_COMPANY_NAME ="company_name";
	public static final String KEY_TICKER_SYMBOL ="ticker_symbol";
	public static final String KEY_USER_ID ="user_id";
	public static final String KEY_USER_NAME ="user_name";

	//DB Variables
	private static final String DATABASE_NAME =DataConstants.DATABASE_NAME;
	private static final String TABLE_NAME =DataConstants.PORTFOLIO_TABLE;
	private static final int DB_VERSION = 1;
	
	private DataBaseHelper helper;
	private final Context ctx;
	private SQLiteDatabase database;
	
	public PortFolioDB(Context ct)
	{
		ctx = ct;
	}
	
	public PortFolioDB open() throws SQLException
	{
		helper = new DataBaseHelper(ctx,DATABASE_NAME, null, DB_VERSION);
		database = helper.getWritableDatabase();
		return this;
	}
	
	public void close()
	{
		helper.close();
	}

	public long addData(String tickerSymbol,String companyName,int user_id,String user_Name) {
		
		ContentValues cv = new ContentValues();
		cv.put(KEY_TICKER_SYMBOL, tickerSymbol);
		cv.put(KEY_COMPANY_NAME, companyName);
		cv.put(KEY_USER_ID, user_id );
		cv.put(KEY_USER_NAME, user_Name);
		
		return database.insert(TABLE_NAME, null, cv);		
	}
	
	public boolean removeData(String tickerSymbol,String companyName,int user_Id,String user_Name)
	{
		try
		{
			/*database.execSQL("DELETE FROM " + TABLE_NAME +" where " + KEY_USER_ID +" = " + 
					user_Id + " AND " + KEY_COMPANY_NAME + " = " + companyName + ";");*/
			
			
			database.delete(TABLE_NAME, KEY_USER_ID +" = " + 
					user_Id + " AND " + KEY_COMPANY_NAME + " = '" + companyName + "'", null);
			return true;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}		
	}

	public void deleteAllData()
	{
		database.delete(TABLE_NAME, "1", null);
	}
	
	public List<UserPortfolio> getPortFolioList(int user_id) {
		String [] columns = new String[] {KEY_ROWID, KEY_USER_ID,KEY_USER_NAME,KEY_TICKER_SYMBOL,KEY_COMPANY_NAME};
		Cursor cur = database.query(TABLE_NAME, columns, null, null, null, null, null);
		List<UserPortfolio> result = new ArrayList<UserPortfolio>();
				
		for(cur.moveToFirst(); !cur.isAfterLast();cur.moveToNext())
		{
			UserPortfolio data = new UserPortfolio();
			data.setUserId(cur.getString(cur.getColumnIndex(KEY_USER_ID)));
			data.setUserName(cur.getString(cur.getColumnIndex(KEY_USER_NAME)));
			data.setTickerSymbol(cur.getString(cur.getColumnIndex(KEY_TICKER_SYMBOL)));
			data.setCompanyName(cur.getString(cur.getColumnIndex(KEY_COMPANY_NAME)));
			result.add(data);			
		}
		
		return result;
	}
}
